// //index.js
import store from "../../utils/factory";
import {
  getRecommendList,
  getBannerList,
  getCategoryList,
  notice,
  home,
  getShopCarList,
  shopCarAdd
} from "../../utils/config";

//获取应用实例
const app = getApp();
let that = null;

Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === "function" && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        });
        this.getTabBar().setData({
          count: 0
        });
        if (store.get("userInfo")) {
          this.getTotal();
        }
      }
      wx.showLoading({
        title: "加载中..."
      });
      if (store.get("wholeSale")) {
        this.setData({
          isShowButton: store.get("wholeSale")
        });
        console.log(store.get("wholeSale"));
      }
      this.getCategoryList();
      // this.getRecommendList();
      // this.getSuperValueExplosion()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    bannerList: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    goodsH: 0,
    isShowButton: "off",
    interval: 5000,
    duration: 500,
    detailObj: {},
    notice: "",
    dialog1: true,
    animationData: {},
    scrollTop: 0,
    isFly: false,
    largeClassList: [],
    topList: [{
        id: 1,
        name: "今日配送",
        src: "../../images/index_icon_today@3x.png"
      },
      {
        id: 2,
        name: "历史配送",
        src: "../../images/index_icon_history@3x.png"
      },
      {
        id: 3,
        name: "审核过滤",
        src: "../../images/index_icon_shenhe@3x.png"
      },
      {
        id: 4,
        name: "未配送",
        src: "../../images/index_icon_waittosend@3x.png"
      },
      {
        id: 5,
        name: "配送中",
        src: "../../images/index_icon_waittotake@3x.png"
      },
      {
        id: 6,
        name: "已配送",
        src: "../../images/index_icon_done@3x.png"
      },
      {
        id: 7,
        name: "已拒绝",
        src: "../../images/index_icon_refuse@3x.png"
      }
    ],
    pageInfo: {
      page: 1,
      limit: 20,
      type: 2 //小类 1 大类
    },
    pageInfo: {
      page: 1,
      limit: 99,
      name: "每日推荐",
      type: 2 //小类 1 大类
    },
    pageInfo2: {
      page: 1,
      limit: 99,
      name: "超值爆款",
      type: 2 //小类 1 大类
    },
    note: [],
    remmond: []
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function () {
    that = this;
    this.notice();
  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function () {
    let systemInfo = wx.getStorageSync("systemInfo");
    this.setData({
      goodsH: systemInfo.windowHeight - 50
    });
    wx.getLocation({
      success: res => {
        console.log(res);
      }
    });
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onShareAppMessage() {
      return {
        title: "天渡大客户基地",
        path: '/pages/index/index'
      }
    },
    getRecommendList() {
      getRecommendList(this.data.pageInfo).then(res => {
        let req = res.data;
        that.setData({
          note: req
        });
        wx.hideLoading();
      });
    },
    goHistory() {
      if (!store.get("userInfo")) {
        wx.navigateTo({
          url: "../autoLogin/autoLogin" // 页面 B
        });
        return false;
      }
      wx.navigateTo({
        url: `../historyOrder/historyOrder`
      });
    },
    getSuperValueExplosion() {
      getRecommendList(this.data.pageInfo2).then(res => {
        let req = res.data;
        that.setData({
          remmond: req
        });
      });
    },
    goOrderList(e) {
      const id = e.currentTarget.dataset.id;
      if (!store.get("userInfo")) {
        wx.navigateTo({
          url: "../autoLogin/autoLogin" // 页面 B
        });
        return false;
      }
      wx.navigateTo({
        url: `../order/order?id=${id}`
      });
    },
    getTotal() {
      getShopCarList().then(res => {
        let req = res.data;
        this.getTabBar().setData({
          count: req.quantity
        });
      });
    },
    goClassify(e) {
      const name = e.currentTarget.dataset.text;
      store.set("currentName", name);
      console.log(name);
      wx.switchTab({
        url: "../classify/classify"
      });
    },
    toMini() {
      console.log("111");
      wx.navigateToMiniProgram({
        appId: "wx7c513e89ef013b59", //小程序appid
        path: "pages/index/index",
        extraData: {
          //需要传递给目标小程序的数据，目标小程序可在 App.onLaunch()，App.onShow() 中获取到这份数据。
          foo: "bar"
        },
        //**重点**要打开的小程序版本，有效值 develop（开发版），trial（体验版），release（正式版）
        envVersion: "release",
        success(res) {
          // 打开成功
        },
        fail(err) {
          console.log(err);
        }
      });
    },
    goDetail(e) {
      const id = e.currentTarget.dataset.id;
      console.log(id);
      wx.navigateTo({
        url: `../logs/logs?id=${id}`
      });
    },
    //监听屏幕滚动 判断上下滚动
    onPageScroll: function (ev) {
      this.setData({
        scrollTop: ev.scrollTop > 200 ? 1 : ev.scrollTop / 200
      });
    },
    goSearch() {
      wx.navigateTo({
        url: `../search/search`
      });
    },
    goBanner(e) {
      console.log("111");
      const id = e.currentTarget.dataset.id;
      console.log(id);
      if (id) {
        wx.navigateTo({
          url: `../logs/logs?id=${id}`
        });
      }
    },
    addShopCar: function (e) {
      console.log(e);
      if (!store.get("userInfo")) {
        wx.navigateTo({
          url: "../autoLogin/autoLogin" // 页面 B
        });
        return false;
      }
      let detailObj = e.currentTarget.dataset.obj;
      if (!detailObj) {
        wx.showToast({
          title: "该商品不存在",
          icon: "none"
        });
        return false;
      }
      if (detailObj.saleOut) {
        wx.showToast({
          title: "该商品已经售罄",
          icon: "none"
        });
        return false;
      }
      shopCarAdd({
        category: detailObj.id,
        quantity: 1
      }).then(res => {
        wx.showToast({
          title: "添加购物车成功",
          icon: "none",
          duration: 1000
        });
        this.getTabBar().setData({
          count: parseInt(this.getTabBar().data.count) + 1
        });
      });
    },
    notice() {
      notice().then(res => {
        let req = res.data;
        that.setData({
          notice: req
        });
      });
    },
    close() {
      this.setData({
        dialog1: true
      });
    },
    getCategoryList() {
      getCategoryList({
        page: 1,
        limit: 8,
        home: "on"
      }).then(res => {
        let req = res.data;
        this.setData({
          largeClassList: req
        })
        wx.hideLoading()
        console.log(this.data.largeClassList)
      });
    }
  }
});